// pages/major/major.js
const app = getApp()
const wxRequest = require('../../utils/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    showChoose:true,
    majors:['电子信息','电子信息','电子信息'],
    majorList:[],
    schoolindex:-1,
    showSearch:true
    // schoolindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    // let pageSize = that.data.pageSize;
    let pageSize = 10;
    // let pageNum = that.data.pageNum;
    let pageNum = 1;
    that.getUserData(that,pageSize,pageNum)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  confirm(e){
    this.setData({
      showChoose:true
    })
  },
  clickchoose(e){
    if(e.detail.showChoose==false && !this.data.majorList.length>0){
      this.getMajorList()
    }
    this.setData({
      showChoose:e.detail.showChoose
    })
  },
  
  close(e){
    this.setData({
      showChoose:true
    })
  },
  clickMajor(e){
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/majorinfo/majorinfo?id='+id,
    })
  },
  search(e) {
    let majorName = e.detail.searchVal;
    this.setData({
      pageNum: 1,
      showSearch:false
    })
    this.getSearchMajorList(majorName)
    
  },
  clickShowSearch(e){
    this.setData({
      showSearch:false
    })
  },
  clearVal(e){
    this.setData({
      showSearch:true
    })
  },
  getUserData (that) {
    app.globalData.wxRequest.requestGet('major/getListGroup.do', {  }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        majorInfo: res.data,
        nodata:false
      })
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
  showMajor(e){
    let index = this.data.schoolindex == e.currentTarget.dataset.index ? -1:e.currentTarget.dataset.index
    this.setData({
      schoolindex:index
    })
  },
  getMajorList(){
    let that = this;
    wxRequest.requestGet('major/getList.do',{}).then(res=>{
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        majorList : res.data
      })
    }).catch(err=>{
      wx.showToast({
        title: '请求失败',
        icon:'error'
      })
    })
  },
  getSearchMajorList(majorName){
    let that = this;
    wxRequest.requestGet('major/getMajorList.do',{"majorName":majorName}).then(res=>{
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        searchMajorList : res.data
      })
    }).catch(err=>{
      wx.showToast({
        title: '请求失败',
        icon:'error'
      })
    })
  }
})